package com.android.lyricshow.adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.lyricshow.R;
import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.ui.ListItemListener;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SongAdapter extends BaseAdapter {
	
	private Context mContext 		= null;
	private List<SongBean> mSongList= null;
	
	private ListItemListener mListener = null;
	
	public SongAdapter(Context context, ArrayList<SongBean> list){
		
		mContext	= context;
		mSongList	= list;
	}
	
	@Override
	public int getCount() {
		return mSongList.size();
	}

	@Override
	public Object getItem(int index) {
		return mSongList.get(index);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.song_list_item, null);
			
			vh 			= new ViewHolder();
			vh.tvtitle 	= (TextView)convertView.findViewById(R.id.song_title);
			vh.iv		= (ImageView)convertView.findViewById(R.id.add_btn);
			
			convertView.setTag(vh);
		}else{
			vh = (ViewHolder)convertView.getTag();
		}
		
		SongBean sbean = mSongList.get(position);
		
		vh.tvtitle.setText(sbean.getArtist() + "-" + sbean.getTitle());
		
		vh.iv.setOnClickListener(new AddListener(position));
		
		return convertView;
	}
	
	public void setClickItemListener(ListItemListener listener){
		mListener = listener;
	}
	
	class AddListener implements View.OnClickListener{
		
		private int mCurPos;
		
		public AddListener(int pos){
			mCurPos = pos;
		}
		
		@Override
		public void onClick(View v) {
			mListener.OnItemClick(mCurPos);
		}
	}
	
	static class ViewHolder{
		public TextView tvtitle;
		public ImageView iv;
	}

}
